package com.spzx.user.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.api.domain.UserAddress;
import com.spzx.user.domain.UserBrowseHistory;
import com.spzx.user.domain.UserCollect;
import com.spzx.user.domain.vo.BrowseHistoryVo;
import com.spzx.user.domain.vo.CollectSkuVo;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.mapper.UserBrowseHistoryMapper;
import com.spzx.user.mapper.UserCollectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserInfoMapper;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.service.IUserInfoService;

/**
 * 会员Service业务层处理
 *
 * @author atguigu
 * @date 2024-09-28
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService
{
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private UserCollectMapper userCollectMapper;

    @Autowired
    private UserBrowseHistoryMapper userBrowseHistoryMapper;

    @Autowired
    private RemoteProductService remoteProductService;
    /**
     * 查询会员列表
     *
     * @param userInfo 会员
     * @return 会员
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo)
    {
        return userInfoMapper.selectUserInfoList(userInfo);
    }

    @Override
    public List<UserAddress> selectUserAddressList(Long userId) {
        return userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId));
    }

    @Override
    public void register(UserInfo userInfo) {
        // 判断手机号是否已经存在
        long count = userInfoMapper.selectCount(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUsername, userInfo.getUsername()));
        if(count > 0) {
            throw new ServiceException("手机号已经存在");
        }
        userInfo.setStatus(1);
        userInfo.setSex(0);
        userInfo.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");

        userInfoMapper.insert(userInfo);
    }

    @Override
    public UserInfo selectUserByUserName(String username) {
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<>(UserInfo.class)
                .eq(UserInfo::getUsername, username));
        return userInfo;
    }

    @Override
    public Boolean updateUserLogin(UpdateUserLogin updateUserLogin) {
        UserInfo userInfo = new UserInfo();
        // 封装数据
        userInfo.setId(updateUserLogin.getUserId());
        userInfo.setLastLoginIp(updateUserLogin.getLastLoginIp());
        userInfo.setLastLoginTime(updateUserLogin.getLastLoginTime());
        int flag = userInfoMapper.updateById(userInfo);
        return flag > 0;
    }

    @Override
    public Boolean collect(Long skuId) {
        // 获取当前登录用户的id
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();
        // 封装数据对象
        UserCollect userCollect = new UserCollect();
        userCollect.setUserId(userId);
        userCollect.setSkuId(skuId);
        userCollect.setCreateBy(username);
        // 根据userId和skuId添加收藏
        int i = userCollectMapper.insert(userCollect);
        return i > 0;
    }

    @Override
    public Boolean cancelCollect(Long skuId) {
        // 获取当前登录用户的id
        Long userId = SecurityUtils.getUserId();
        // 根据userId和skuId删除收藏
        int i = userCollectMapper.delete(new LambdaQueryWrapper<UserCollect>()
                .eq(UserCollect::getUserId, userId)
                .eq(UserCollect::getSkuId, skuId));
        return i > 0;
    }

    @Override
    public List<CollectSkuVo> selectUserCollectList() {
        // 查询当前登录用户的收藏列表
        Long userId = SecurityUtils.getUserId();
        // 获取用户的收藏列表
        List<UserCollect> userCollectList = userCollectMapper.selectList(new LambdaQueryWrapper<UserCollect>()
                .eq(UserCollect::getUserId, userId));
        // 从收藏列表中取出skuId
        List<Long> skuIdList = userCollectList.stream().map(UserCollect::getSkuId).toList();
        // 远程调用product-api 查询sku信息
        List<CollectSkuVo> collectSkuVoList = new ArrayList<>();
        for (Long skuId : skuIdList) {
            CollectSkuVo collectSkuVo = new CollectSkuVo();
            ProductSku productSku = remoteProductService.getProductSku(skuId, SecurityConstants.INNER).getData();
            collectSkuVo.setSkuId(productSku.getId());
            BeanUtils.copyProperties(productSku, collectSkuVo);
            collectSkuVoList.add(collectSkuVo);
        }

        return collectSkuVoList;
    }

    @Override
    public Boolean isCollect(Long skuId) {
        // 获取当前登录用户的id
        Long userId = SecurityUtils.getUserId();
        // 根据userId和skuId查询收藏
        UserCollect userCollect = userCollectMapper.selectOne(new LambdaQueryWrapper<UserCollect>()
                .eq(UserCollect::getUserId, userId)
                .eq(UserCollect::getSkuId, skuId));
        if (userCollect == null){
            return false;
        }
        return true;
    }

    @Override
    public List<BrowseHistoryVo> userBrowseHistoryList() {
        // 查询当前登录用户的收藏列表
        Long userId = SecurityUtils.getUserId();
        // 查询当前登录用户的浏览历史列表
        List<UserBrowseHistory> userBrowseHistoryList = userBrowseHistoryMapper.selectList(new LambdaQueryWrapper<UserBrowseHistory>()
                .eq(UserBrowseHistory::getUserId, userId));
        // 从浏览历史列表中取出skuId
        List<Long> skuIdList = userBrowseHistoryList.stream().map(UserBrowseHistory::getSkuId).toList();
        // 远程调用product-api 查询sku信息
        List<BrowseHistoryVo> BrowseHistoryVoList = new ArrayList<>();
        for (Long skuId : skuIdList) {
            BrowseHistoryVo browseHistoryVo = new BrowseHistoryVo();
            ProductSku productSku = remoteProductService.getProductSku(skuId, SecurityConstants.INNER).getData();
            browseHistoryVo.setSkuId(productSku.getId());
            BeanUtils.copyProperties(productSku, browseHistoryVo);
            BrowseHistoryVoList.add(browseHistoryVo);
        }
        return BrowseHistoryVoList;
    }

    @Override
    public Boolean addBrowseHistory(Long skuId) {
        // 获取当前登录用户的id
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();
        // 封装数据对象
        UserBrowseHistory userBrowseHistory = new UserBrowseHistory();
        userBrowseHistory.setUserId(userId);
        userBrowseHistory.setSkuId(skuId);
        userBrowseHistory.setCreateBy(username);
        // 根据userId和skuId添加收藏
        int i = userBrowseHistoryMapper.insert(userBrowseHistory);
        return i > 0;
    }

}
